package cn.yh.register.msm.service.impl;

import cn.yh.register.common.exception.RegisterException;
import cn.yh.register.common.result.ResultCodeEnum;
import cn.yh.register.msm.service.MsmService;
import cn.yh.register.msm.util.ConstantPropertiesUtils;
import cn.yh.register.vo.msm.MsmVo;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {


    /**
     * 发送验证码
     *
     * @param phone 手机号
     * @param code  验证码
     * @return Boolean
     */
    @Override
    public Boolean send(String phone, String code) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        return this.send(phone, map);
    }

    /**
     * 发送消息
     * @param msmVo 封装的消息对象
     * @return 是否发送成功
     */
    @Override
    public boolean send(MsmVo msmVo) {
        return this.send(msmVo.getPhone(), msmVo.getParam());
    }
    private Boolean send(String phone,Map<String,Object> param){
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(param)) {
            throw new RegisterException(ResultCodeEnum.PARAM_ERROR);
        }
        //创建一个profile对象
        DefaultProfile profile = DefaultProfile.getProfile(ConstantPropertiesUtils.REGION_Id,
                ConstantPropertiesUtils.ACCESS_KEY_ID,
                ConstantPropertiesUtils.SECRECT);
        //创建一个客户端对象
        IAcsClient client = new DefaultAcsClient(profile);

        //创建发送短信的请求对象
        SendSmsRequest request = new SendSmsRequest();
        //签名名称
        request.setSignName("阿里云短信测试");
        //模板code
        request.setTemplateCode("SMS_154950909");
        //手机号
        request.setPhoneNumbers(phone);
        //模板参数
//        request.setTemplateParam("{\"code\":\"1234\"}");

        // Map<String, Object> map = new HashMap<>();
        // map.put("code", code);
        request.setTemplateParam(JSONObject.toJSONString(param));
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            System.out.println(response.getCode());
//            if ("OK".equals(response.getCode())) {
//            }
            System.out.println("已向手机号："+phone+"发送短信："+JSONObject.toJSONString(param));
            return true;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return false;
    }
}
